/*
 * Copyright (c) 2015 Messcat. All rights reserved.
 */
package cc.messcat.web.system;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.model.Pager;
import cc.messcat.common.model.ResponseBean;
import cc.messcat.common.util.BeanUtil;
import cc.messcat.common.util.LogUtil;
import cc.messcat.common.util.StringUtil;
import cc.messcat.entity.system.Role;
import cc.messcat.log.OperationLog;
import cc.messcat.service.system.RoleService;

/**
 * 角色模块
 *
 * @author Panda
 * @version 1.0
 */
@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "getRoleAll", method = RequestMethod.GET)
	@ResponseBody
	public Object getRoleAll(Integer pageNo, Integer pageSize,HttpSession session) throws Exception {
//		McUser user = (McUser) session.getAttribute("user");
//		Long areaId = user.getAreaId();
//		UserLevel userLevel = user.getUserLevel();
//		if(NumberUtil.isLongZero(areaId)&&NumberUtil.isIntegerZero(userLevel.getLevel())){
			Pager<Role> pager = roleService.retrieveRoleAll(pageNo, pageSize);
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS, pager);
//		}else{
//			//TODO
//			List<Role> roles = new ArrayList<>();
//			int level = userLevel.getLevel();
//			while (level < 5) {
//				List<Area> areas3 = (List<Area>) session.getAttribute("area" + level);
//				for(Area a : areas3){
//					roles.addAll(roleService.retrieveRoleByArea(a));
//				}
//				level++;
//			}
//			Pager<Role> pager = roleService.getPager(pageNo, pageSize, roles, null);
//			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS, pager);
//		}
	}
	
	
	@RequestMapping(value = "getRoleAllFilter", method = RequestMethod.GET)
	@ResponseBody
	public Object getRoleAllFilter(Integer pageNo, Integer pageSize,HttpSession session) throws Exception {
		Pager<Role> pager = roleService.retrieveRoleAllFilter(pageNo, pageSize);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS, pager);
	}
	

	@RequestMapping(value = "getRole/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Object getRole(@PathVariable Long id) throws Exception {
		Role role = roleService.retrieveRoleById(id);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS, role);
	}

	@RequestMapping(value = "addRole", method = RequestMethod.POST)
	@ResponseBody
	@OperationLog(desc="添加角色",module="系统配置")
	public Object addRole(Role role, Long[] permissionss) throws Exception {
		if(role == null || StringUtil.isBlank(role.getName()) || StringUtil.isBlank(role.getStatus())){
			return new ResponseBean(CommonConstant.FAIL_CODE_400, CommonConstant.MSG_PARAMETER_EMPTY);
		}
		if(role.getName().length() > 30){
			return new ResponseBean(CommonConstant.FAIL_CODE_400, "角色名称不能大于30个字符");
		}
		roleService.addRole(role, permissionss);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_ADD_SUCCESS);
	}

	@OperationLog(desc="删除角色",module="系统配置")
	@RequestMapping(value = "delRole/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Object delRole(@PathVariable Long id) throws Exception {
		Long removeId = roleService.removeRole(id);
		if(removeId == -1) {
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, "该角色已绑定用户，无法删除");
		}
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_DELETE_SUCCESS);
	}

	@RequestMapping(value = "updateRole", method = RequestMethod.POST)
	@OperationLog(desc="跟新角色",module="系统配置")
	@ResponseBody
	public Object updateRole(Role role, Long[] permissionss) throws Exception {
		if(role == null || StringUtil.isBlank(role.getName()) || StringUtil.isBlank(role.getStatus())){
			return new ResponseBean(CommonConstant.FAIL_CODE_400, CommonConstant.MSG_PARAMETER_EMPTY);
		}
		if(role.getName().length() > 30){
			return new ResponseBean(CommonConstant.FAIL_CODE_400, "角色名称不能大于30个字符");
		}
		roleService.modifyRole(role, permissionss);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_UPDATE_SUCCESS);
	}

	@RequestMapping(value = "/isExistRolesName", method = RequestMethod.POST)
	@ResponseBody
	public Object isExistRolesName(String roles,Long id) {
		try {
			if (BeanUtil.isValid(roles)) {
				boolean flag=roleService.isExistRoles(roles, id);
				if(flag){
					return new ResponseBean(CommonConstant.SUCCESS_CODE_200, "此角色名可用",flag);
				}else{
					return new ResponseBean(CommonConstant.SUCCESS_CODE_200, "此角色名已存在",flag);
				}
			} else {
				return new ResponseBean(CommonConstant.FAIL_CODE_400, CommonConstant.MSG_PARAMETER_EMPTY);
			}
		} catch (Exception e) {
			LogUtil.logException("RoleController isExistRolesName has exception:", e);
			e.printStackTrace();
			return new ResponseBean(CommonConstant.EXCEPTION_CODE_500, CommonConstant.MSG_FAIL);
		}
	}

}